package com.zzzy.doc.service;

import com.zzzy.doc.dao.entity.DocAdvice;
import com.zzzy.doc.dao.entity.Document;
import com.zzzy.doc.dao.mapper.DocAdviceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzzy.framework.common.util.SecurityUtils;
import com.zzzy.framework.core.mybatis.query.LambdaQueryWrapperE;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 公文各阶段意见
 *
 * @author fc
 * @since 2024-08-20
 */
@Service
public class DocAdviceService extends ServiceImpl<DocAdviceMapper, DocAdvice> {

    @Resource
    private DocService docService;

    public void addAdvice(Long docId, String message) {
        Document document = docService.getDocByIdForQuery(docId);
        DocAdvice docAdvice = new DocAdvice();
        docAdvice.setDocId(docId);
        docAdvice.setOperatorName(SecurityUtils.getLoginUser().getNickName());
        docAdvice.setStage(document.getStatus());
        docAdvice.setContent(message);
        docAdvice.setCreateTime(LocalDateTime.now());
        baseMapper.insert(docAdvice);
    }

    public void addAdvice(Long docId) {
        addAdvice(docId, "");
    }

    public List<DocAdvice> getAdviceListDocId(Long docId) {
        docService.validateAuthForQuery(docId);
        return baseMapper.selectList(new LambdaQueryWrapperE<DocAdvice>().eq(DocAdvice::getDocId, docId));
    }
}
